<?php

namespace App\Library\Service;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Logic\ShopProductSpecsLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Product\ProductAddRequest;
use App\Library\Requests\Product\ProductBuyWillListRequest;
use App\Library\Requests\Product\ProductBuyWillSpecsListRequest;
use App\Library\Requests\Product\ProductEditRequest;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;

class ProductService extends BaseService
{
    /**
     * @param ShopProductLogic $logic
     */
    public function __construct(ShopProductLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * add
     * @param ProductAddRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function add(ProductAddRequest $request): JsonResponse
    {
        //if (ShopProductSpecsLogic::checkSave($request)) return failMessage('规格不存在');
        //else if (ShopImagesLogic::checkProductMain($request)) return failMessage('主图不存在');
        //else if (ShopImagesLogic::checkProductDesc($request)) return failMessage('详情图不存在');
        //else if (ShopImagesLogic::checkProductVideo($request)) return failMessage('视频不存在');
        mq('shopProductChange', $this->logic->add($request));
        OperateLogLogic::add();
        return success();
    }

    /**
     * edit
     * @param ProductEditRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function edit(ProductEditRequest $request): JsonResponse
    {
        $item = $this->checkItem($request->serialNumber);
        if ($item instanceof JsonResponse) return $item;
        //else if (ShopProductSpecsLogic::checkSave($request)) return failMessage('规格不存在');
        //else if (ShopImagesLogic::checkProductMain($request, $item->product_id)) return failMessage('主图不存在');
        //else if (ShopImagesLogic::checkProductDesc($request, $item->product_id)) return failMessage('详情图不存在');
        //else if (ShopImagesLogic::checkProductVideo($request, $item->product_id)) return failMessage('视频不存在');
        mq('shopProductChange', $this->logic->edit($request, $item));
        OperateLogLogic::add();
        return success();
    }

    /**
     * delete
     * @param SerialNumberRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function delete(SerialNumberRequest $request): JsonResponse
    {
        $item = $this->checkItem($request->serialNumber);
        if ($item instanceof JsonResponse) return $item;
        else if ($item->product_status === 2) return failMessage('已上架商品无法删除');
        $item->deleted_at = now();
        $item->save();
        OperateLogLogic::add();
        return success();
    }

    /**
     * copy
     * @param SerialNumberRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function copy(SerialNumberRequest $request): JsonResponse
    {
        $item = $this->checkItem($request->serialNumber);
        if ($item instanceof JsonResponse) return $item;
        else return success($this->logic->copy($item)->product_id);
    }

    /**
     * adminTj
     * @return JsonResponse
     */
    public function adminTj(): JsonResponse
    {
        return success($this->logic->adminTj());
    }

    /**
     * checkItem
     * @param string $serialNumber
     * @return Model|JsonResponse
     */
    private function checkItem(string $serialNumber): Model|JsonResponse
    {
        $item = $this->logic->findBySerialNumber($serialNumber);
        if (is_null($item)) return fail(433);
        else if (USERID === MAINID || POWER === 1) {
            if ($item->user_id !== MAINID) return fail(433);
        } else if ($item->user_id !== USERID) return fail(433);
        return $item;
    }

    public function buyWillList(ProductBuyWillListRequest $request): JsonResponse
    {
        $shopUserId = array_filter(explode('-', aesDecrypt($request->sellerId)), function ($v) {
            return $v !== '';
        });
        return success($this->logic->buyWillList($shopUserId, $request->productsId));
    }

    /**
     *
     * @param ProductBuyWillSpecsListRequest $request
     * @return JsonResponse
     *
     */
    public function buyWillSpecsList(ProductBuyWillSpecsListRequest $request): JsonResponse
    {
        return success(ShopProductSpecsLogic::buyWillSpecsList($request));
    }
}
